package br.utfpr.edu.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.primefaces.model.DualListModel;

import br.utfpr.edu.bean.Classes;
import br.utfpr.edu.bean.FamiliaRel;
import br.utfpr.edu.business.Fabrica;
import br.utfpr.edu.business.Negocio;
import br.utfpr.edu.business.NegocioException;

public class RelatorioDao extends Negocio{
	
	private static final long serialVersionUID = 1995990365936613286L;
	private final String CONSULTA_CLASSE = "SELECT a.classe,count(a.classe)FROM Analise a WHERE a.gene.id = ?1 GROUP BY a.classe ORDER BY a.classe";
	private final String CONSULTA_FAMILIA = "SELECT a.familia.descricao,a.familia.id,COUNT(a.familia.id)FROM Analise a INNER JOIN a.familia f WHERE a.gene.id = ?1 GROUP BY a.familia.id,a.familia.descricao";
    private final String CONSULTA_TAMANHO_CLASSE = "SELECT a.classe,sum(a.fim - a.inicio) + 1 FROM Analise a WHERE a.gene.id = ?1 GROUP BY a.classe ORDER BY a.classe"; 
	private final String CONSULTA_TAMANHO_FAMILIA = "SELECT a.familia.descricao,a.familia.id,sum(a.fim - a.inicio)+1 FROM Analise a WHERE a.gene.id = ?1 GROUP BY a.familia.id,a.familia.descricao";
    
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ArrayList<Classes> pesquisarClasses(DualListModel<String> colecao,String tipoHistograma)throws NegocioException{
		erros.limpar();
		
		EntityManager em = Fabrica.getInstance().getEntityManager();
		Query qr = null;
		
		if(tipoHistograma.equals("1")){
			qr = em.createQuery(CONSULTA_CLASSE);
		}else{
			qr = em.createQuery(CONSULTA_TAMANHO_CLASSE);
		}
		
		String[]projetoGene;
		List<Object> col  = null;
		Long sine = new Long(0);
		Long line = new Long(0);
		Long ltr = new Long(0);
		Long dna = new Long(0);
		
		for(String item : colecao.getTarget()){
			 projetoGene = item.split(" - ");
			 qr.setParameter(1,Integer.parseInt(projetoGene[1]));
			 col = qr.getResultList();
			 Iterator it = col.iterator();
			 while(it.hasNext()){
				Object[] result = (Object[])it.next();
				String nome = (String)result[0];
				if(nome.equalsIgnoreCase("DNA")){
					dna += (Long)result[1];
				}else if(nome.equalsIgnoreCase("SINE")){
					sine += (Long)result[1];
				}else if(nome.equalsIgnoreCase("LINE")){
					line += (Long)result[1];
				}else if(nome.equalsIgnoreCase("LTR")){
					ltr += (Long)result[1];
				}
			 }
		}
		
		Classes csine = new Classes();
		csine.setQtde(String.valueOf(sine));
		csine.setDescricao("SINE");
		Classes cline = new Classes();
		cline.setQtde(String.valueOf(line));
		cline.setDescricao("LINE");
		Classes cltr = new Classes();
		cltr.setQtde(String.valueOf(ltr));
		cltr.setDescricao("LTR");
		Classes cdna = new Classes();
		cdna.setQtde(String.valueOf(dna));
		cdna.setDescricao("DNA");
		ArrayList<Classes> colClasse = new ArrayList<Classes>();
		colClasse.add(csine);
		colClasse.add(cline);
		colClasse.add(cltr);
		colClasse.add(cdna);
		
	    em.getTransaction().commit();
	    em.close();
		return colClasse;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ArrayList<FamiliaRel> pesquisarFamilia(DualListModel<String> colecao, String tipoHistograma)throws NegocioException{
		erros.limpar();
		
		EntityManager em = Fabrica.getInstance().getEntityManager();
		Query qr = null;
		
		if(tipoHistograma.equals("1")){
			qr = em.createQuery(CONSULTA_FAMILIA);
		}else{
			qr = em.createQuery(CONSULTA_TAMANHO_FAMILIA);
		}
		
		String[]projetoGene;
		List<Object> col  = null;
		ArrayList<FamiliaRel> colecaoFamilia = new ArrayList<FamiliaRel>();
		
		for(String item : colecao.getTarget()){
			 projetoGene = item.split(" - ");
			 qr.setParameter(1,Integer.parseInt(projetoGene[1]));
			 col = qr.getResultList();
			 Iterator it = col.iterator();
			 while(it.hasNext()){
				Object[] result = (Object[])it.next();
				String descricao = (String)result[0];
				int id = (Integer)result[1];
				long qtde = (Long)result[2];
				FamiliaRel f = new FamiliaRel();
				f.setDescricao(descricao);
				f.setId(String.valueOf(id));
				f.setQtde(String.valueOf(qtde));
				
				if(!colecaoFamilia.contains(f)){
					colecaoFamilia.add(f);
				}else{
					for(FamiliaRel fa : colecaoFamilia){
						if(fa.equals(f)){
							fa.setQtde(String.valueOf(Integer.parseInt(fa.getQtde())+Integer.parseInt(f.getQtde())));
						}
					}
				}
			 }
		}

		em.getTransaction().commit();
	    em.close();
		return colecaoFamilia;
	}

	@Override
	public void limpar() {
		// TODO Auto-generated method stub
		
	}
}
